package com.darkplanet.management.controller;

import com.darkplanet.management.domain.Employee;
import com.darkplanet.management.domain.SecondMenu;
import com.darkplanet.management.service.EmployeeMenuImp;
import com.darkplanet.management.service.EmployeeRoleService;
import com.darkplanet.management.service.EmployeeServiceImp;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.List;

@WebServlet(urlPatterns = "/UserAddEditServlet")
public class UserAddEditServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request,response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //添加或修改用户
        HttpSession session = request.getSession();
        if(session.getAttribute("employee")!=null){
            List menus1 = (List) session.getAttribute("Menus");
            List<SecondMenu> menuList = (List<SecondMenu>) menus1.get(1);
            for (SecondMenu secondMenu : menuList) {
                if(secondMenu.getS_menu_id()==13){
                    String position = request.getParameter("position");
                    String name = request.getParameter("name");
                    String gender = request.getParameter("gender");
                    Integer age = Integer.parseInt(request.getParameter("age"));
                    String phone = request.getParameter("phone");
                    String date = request.getParameter("date");
                    String id_card = request.getParameter("id_card");
                    String remark = request.getParameter("remark");
                    String url_to = request.getParameter("url_to");
                    EmployeeServiceImp employeeService = new EmployeeServiceImp();
                    Integer employee_id = null;
                    if (url_to.equals("0")) {
                        employeeService.addEmployee(position, name, gender, age, phone, date, id_card, remark);
                        employee_id = employeeService.Login(name, "123456").getEmployee_id();
                    } else if (url_to.equals("1")) {
                        employee_id = (Integer) session.getAttribute("employee_id");
                        employeeService.updateEmployee(position, name, gender, age, phone, date, id_card, remark, employee_id);
                    }
                    //更新权限
                    String admin = request.getParameter("admin");
                    String manager = request.getParameter("manager");
                    String progammer = request.getParameter("progammer");
                    String[] roles = {admin, manager, progammer};
                    EmployeeRoleService employeeRoleService = new EmployeeRoleService();
                    employeeRoleService.deleteEmployeeRoleById(employee_id);
                    for (String role : roles) {
                        if (role != null) {
                            employeeRoleService.addEmployeeRoleById(employee_id, Integer.parseInt(role));
                        }
                    }
                    if (url_to.equals("1")) {
                        Employee employee1 = (Employee) session.getAttribute("employee");
                        if (employee_id == employee1.getEmployee_id()) {
                            Employee employee = employeeService.getEmployeeBYId(employee_id);
                            session.setAttribute("employee", employee);
                            EmployeeMenuImp employeeMenu = new EmployeeMenuImp();
                            List<List> menus = employeeMenu.getMenus(employee.getEmployee_id());
                            session.setAttribute("Menus", menus);
                        }
                    }
                    List<Employee> employees = employeeService.getEmployees();
                    request.getSession().setAttribute("userlist",employees);
                    response.sendRedirect(request.getContextPath()+"/user.jsp");
                }
                }
            }
    }
}
